package qqtalk.moshi;

import java.util.*;
import java.net.*;
import java.io.*;
import qqtalk.gonggong.*;
import qqtalk.gongju.Glxc;
import qqtalk.jiemian1.qunliao;

import javax.swing.*;

public class Xiancheng extends Thread
{
    private Socket s;
    private JTextArea parentArea;//服务器面板
    private List<String> userList;

    private qunliao parentUI;
    private static final int BUFSIZE=8096; //缓冲区大小

    public Xiancheng(Socket s)
    {
        this.s=s;
    }

    public Xiancheng(Socket s,JTextArea area,List<String> u)
    {
        this.s=s;
        this.parentArea=area;
        this.userList=u;
    }

    public void tongzhi(String iam)
    {
        HashMap hm=Xianchengbaioshi.hm;
        Iterator it=hm.keySet().iterator();
        while(it.hasNext())
        {
            Message m=new Message();
            m.setCon(iam);
            m.setMesType(MessageType.message_ret_onLineFriend);
            String onLineUserId=it.next().toString();
            try {
                ObjectOutputStream oos=new ObjectOutputStream(Xianchengbaioshi.getxiancheng(onLineUserId).s.getOutputStream());
                m.setGetter(onLineUserId);
                oos.writeObject(m);
            } catch (Exception e) {}
        }
    }
    public void run()
    {
        while(true)
        {
            try {
                ObjectInputStream ois=new ObjectInputStream(s.getInputStream());
                Message m=(Message)ois.readObject();
                if (m.getMesType().equals(MessageType.message_file))
                {
                    String s=m.getGetter()+"发送文件:"+m.getCon();
                    System.out.println(s);//往服务器面板上追加群聊消息;

                    File file=new File("C:\\Users\\cheny\\Desktop");
                    for (int i=0;i<userList.size();i++) {
                        try{
                            Socket cs = Glxc.getLjfwqxc(userList.get(i)).getS();
                            ObjectOutputStream oos = new ObjectOutputStream(cs.getOutputStream());
                            Message me = new Message();
                            me.setCon(m.getCon());
                            me.setSender(m.getGetter());
                            me.setFile(file);
                            me.setMesType(MessageType.message_file);
                            oos.writeObject(me);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
                else if(m.getMesType().equals(MessageType.message_MSG))
                {
                    String s=m.getGetter()+"说:"+m.getCon();
                    System.out.println(s);//往服务器面板上追加群聊消息;
                    //服务端显示的连接信息会被自动覆盖
//                    Xiancheng xc=Xianchengbaioshi.getxiancheng(m.getGetter());
//                    ObjectOutputStream oos=new ObjectOutputStream(xc.s.getOutputStream());
                    //oos.writeObject(m);

                    //服务器转发消息到群聊面板,未成功
                    parentUI.messArea.append(s);
                    for (int i=0;i<userList.size();i++){
                        try
                        {
                            Socket cs=Glxc.getLjfwqxc(userList.get(i)).getS();
                            ObjectOutputStream oos=new ObjectOutputStream(cs.getOutputStream());
                            Message m1=new Message();
                            m1.setCon(m.getCon());
                            m1.setSender(m.getGetter());
                            m1.setFile(m.getFile());
                            m1.setMesType(MessageType.message_MSG);
                            oos.writeObject(m1);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                }
                else if(m.getMesType().equals(MessageType.message_comm_mes))
                {
                    Xiancheng xc=Xianchengbaioshi.getxiancheng(m.getGetter());
                    ObjectOutputStream oos=new ObjectOutputStream(xc.s.getOutputStream());
                    oos.writeObject(m);
                }
                else if(m.getMesType().equals(MessageType.message_get_onLineFriend))
                {
                    String res=Xianchengbaioshi.getZaixianyonghu();
                    Message m2=new Message();
                    m2.setMesType(MessageType.message_ret_onLineFriend);
                    m2.setCon(res);
                    m2.setGetter(m.getSender());
                    ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
                    oos.writeObject(m2);
                }
            } catch (Exception e) {}
        }
    }
}
